package com.hippo.core.ui.compose.home

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.model.Device
import com.hippo.core.ui.components.BottomSheetDialog
import com.hippo.core.ui.theme.ColorBackgroundWhite
import com.hippo.core.ui.theme.ColorBorder
import com.hippo.core.ui.theme.ColorTextGray
import com.hippo.core.ui.theme.HippoTheme

@Deprecated("这期不做")
@Composable
fun ScanDeviceListDialog(
    deviceList: List<Device>,
    onDismissRequest: () -> Unit,
    onNext: (Device) -> Unit,
    onRefresh: () -> Unit
) {

    BottomSheetDialog(
        title = stringResource(id = R.string.home_6),
        onDismissRequest = onDismissRequest
    ) {
        DialogContent(deviceList, onNext, onRefresh)
    }

}


@Composable
private fun DialogContent(
    deviceList: List<Device>,
    onNext: (Device) -> Unit,
    onRefresh: () -> Unit
) {

    var pickIndex by remember { mutableStateOf(0) }

    Column(
        modifier = Modifier
            .padding(16.dp)
    ) {

        Row {

            Text(
                text = stringResource(R.string.home_7),
                color = ColorTextGray,
                modifier = Modifier.weight(1f)
            )

            Image(
                painter = painterResource(R.mipmap.refresh),
                contentDescription = null,
                modifier = Modifier
                    .size(20.dp)
                    .click {
                        onRefresh()
                    }
            )
        }


        Spacer(modifier = Modifier.height(20.dp))

        LazyColumn(
            modifier = Modifier
                .weight(1F)
        ) {
            itemsIndexed(deviceList) { index, device ->
                ItemDevice(checked = index == pickIndex, device = device) {
                    pickIndex = index
                }
                Spacer(modifier = Modifier.height(10.dp))
            }
        }

        Spacer(
            modifier = Modifier
                .height(20.dp)
        )

        Row {

            Image(
                painter = painterResource(R.mipmap.back_white),
                contentDescription = null,
                modifier = Modifier.size(40.dp)
            )

            Spacer(modifier = Modifier.weight(1f))


            Image(
                painter = painterResource(R.mipmap.next),
                contentDescription = null,
                modifier = Modifier
                    .height(40.dp)
                    .click { onNext(deviceList[pickIndex]) }
            )

        }

        Spacer(
            modifier = Modifier
                .height(20.dp)
        )
    }
}


@Composable
private fun ItemDevice(checked: Boolean, device: Device, onClick: () -> Unit) {


    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .fillMaxWidth()
            .background(
                color = ColorBackgroundWhite,
                shape = MaterialTheme.shapes.medium
            )
            .border(1.dp, ColorBorder, RoundedCornerShape(8.dp))
            .padding(8.dp)
    ) {


        Image(
            painter = painterResource(R.mipmap.machine),
            contentDescription = null,
            modifier = Modifier.size(90.dp)
        )

        Spacer(modifier = Modifier.width(20.dp))

        Column {

            Text(
                text = device.name.orEmpty(),
                color = MaterialTheme.colorScheme.primary,
                style = TextStyle(
                    fontWeight = FontWeight.Bold,
                    fontSize = 16.sp
                )
            )

            Spacer(modifier = Modifier.height(10.dp))

            Text(
                text = stringResource(R.string.home_4),
                color = MaterialTheme.colorScheme.primary,
                fontSize = 14.sp
            )

        }

        Spacer(modifier = Modifier.weight(1f))


        RadioButton(
            selected = checked,
            onClick = onClick,
            modifier = Modifier.padding(20.dp)
        )

    }


}


@Preview(showBackground = true)
@Composable
private fun ScanDeviceListDialogPreview() {
    HippoTheme {
        DialogContent(
            deviceList = listOf(
                Device(
                    id = "1",
                    name = "name",
                    type = "type",
                    status = "status",
                    icon = "icon",
                    isOnline = true,
                    isLocked = true,
                    isMuted = true,
                    isFavorite = true,
                    isSelected = true,
                    isExpanded = true,
                    isChecked = true
                )
            ),
            onNext = {},
            onRefresh = {}
        )
    }
}












